package com.yunkai.plate_lpr;

import android.app.Activity;
import android.content.Intent;

import androidx.annotation.NonNull;

import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

import androidx.annotation.Nullable;

import com.hyperai.hyperlpr3.HyperLPR3;
import com.hyperai.hyperlpr3.bean.HyperLPRParameter;

/** PlateLprPlugin */
public class PlateLprPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  Result plateResult;
  private Activity activity;
  private EventChannel eventChannel;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "com.yunkai.plate_lpr");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    String action = call.method;
    plateResult = result;
    if(action.equals("recog")){
      String type = call.argument("type");
      ImageUtil.setQuality(40);
      Intent intent = new Intent(activity, CraREActivity.class);
      intent.putExtra("type", type);
      activity.startActivityForResult(intent, 200);
//      startActivityForResult(intent, 200);
      return;
    }else if(action.equals("deletePic")){
      ImageUtil.deletePics(3);
    }else if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else {
      result.notImplemented();
    }
  }
  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(ActivityPluginBinding binding) {
    Log.e("onAttachedToActivity", "onAttachedToActivity");
    this.activity = binding.getActivity();

    binding.addActivityResultListener(this::onActivityResult);

    // 车牌识别算法配置参数
    HyperLPRParameter parameter = new HyperLPRParameter().setNmsThreshold(0.92f);
    // 初始化(仅执行一次生效)
    HyperLPR3.getInstance().init(this.activity, parameter);

    Log.i("车牌识别初始化", "已初始化");
//    binding.addActivityResultListener(this::onActivityResult);

  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
    this.activity = null;
  }

  @Override
  public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
    onAttachedToActivity(binding);
  }

  @Override
  public void onDetachedFromActivity() {
    this.activity = null;
  }

  public boolean onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    Log.i("车牌识别返回：", requestCode + "" + resultCode);

    if (requestCode == 200 && resultCode == Activity.RESULT_OK) {
      if (data != null) {
        //获取到的识别结果
        String plate = data.getStringExtra("plate");
        //保存图片路径
        String path = data.getStringExtra("path");
        //是否需要在线识别
        String online = data.getStringExtra("online");

        if (plateResult != null) {
          plateResult.success(plate + ";" + path + ";" + online);
        }
        //            callbackContext.success(plate+"&"+path+"&"+online);
      } else {
        if (plateResult != null) {
          plateResult.success("");
        }
      }
    } else {
      if (plateResult != null) {
        plateResult.success("");
      }
    }
    return true;
  }
}
